#!/bin/sh

if [ $# -lt 2 ]; then
	echo "Usage: $0 <hash type> <num rounds> [<string>]"
	exit 2
fi

input_str=

if [ $# -eq 2 ]; then
	input_str=`cat`
else
	input_str="$3"
fi

for round in `seq 1 $2`; do
	if [ $round -eq 1 ]; then
		input_str=`echo "$input_str" | openssl dgst -$1 | cut -f2 -d' '`
	else
		input_str=`echo "$input_str" | xxd -r -p | openssl dgst -$1 | cut -f2 -d' '`
	fi
done

echo $input_str

#$ ../Projects/affect/scripts/hashround.sh md4 20 'Fastwel*I/O' | xxd -r -p | od -An -tx1 | sed 's/\(.\) /\1,0x/g;s/^ /0x/'

